//
//  SEGDoorQRCodeView.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/7/27.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGDoorQRCodeView.h"

#if __has_include(<Commom/SEGCreateQRCode.h>)
#import <Commom/SEGCreateQRCode.h>
#else
#import "SEGCreateQRCode.h"
#endif

static const CGFloat qrcodeImageWidth = 216;//二维码四面各有16的白边

@interface SEGDoorQRCodeView ()

@property (nonatomic, strong) UIButton *refreshButton;
@property (nonatomic, assign) CGFloat currentBrightness; // 保存当前的屏幕亮度
@property (nonatomic, assign) BOOL isChangedBrightness; // 保存是否进行过亮度修改

@end

@implementation SEGDoorQRCodeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self customUI];
    }
    return self;
}

#pragma mark - 初始化界面

- (void)customUI
{
    self.backgroundColor = kColor_bg3;
//    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenView:)]];
    
    //白色背景视图
    self.contentView = [[UIView alloc] init];
    self.contentView.backgroundColor = kColor_W;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.layer.cornerRadius = 5;
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.width.mas_equalTo(270);
        make.height.mas_equalTo(674/2.0);
    }];

    //图片
    self.iconView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.iconView];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.top.equalTo(self.contentView.mas_top).with.offset(29);
        make.width.mas_equalTo(qrcodeImageWidth);
        make.height.mas_equalTo(qrcodeImageWidth);
    }];
    
    self.refreshButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = UIColor.clearColor;
        [button addTarget:self action:@selector(refreshButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
        button;
    });
    [self.refreshButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.iconView);
    }];
    
    //商品名称
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:13];
    self.nameLabel.textColor = kColor_1;
    self.nameLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.bottom.equalTo(self.contentView).with.offset(-73);
    }];

    //图片
    UIImageView *lineView = [[UIImageView alloc] init];
    lineView.backgroundColor = kColor_L2;
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-44);
        make.width.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
    
    //关闭按钮
    self.closeButton = [[UIButton alloc] init];
    self.closeButton.backgroundColor = [UIColor clearColor];
    self.closeButton.titleLabel.font = kFont_16;
    [self.closeButton setTitleColor:kColor_3 forState:UIControlStateNormal];
    [self.closeButton setTitle:@"关闭" forState:UIControlStateNormal];
    [self.closeButton addTarget:self action:@selector(hiddenView) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.closeButton];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.bottom.equalTo(self.contentView);
        make.centerX.equalTo(self.contentView);
        make.width.mas_equalTo(self.contentView);
        make.height.mas_equalTo(44.0);
    }];
}

- (void)hiddenView:(UITapGestureRecognizer *)tap
{
    if (tap && [tap isKindOfClass:[UITapGestureRecognizer class]]) {
        //点击白色背景不消失
        CGPoint point = [tap locationInView:self];
        if ([self.contentView.layer containsPoint:point]) {
            return;
        }
    }
    self.hidden = YES;
    [self removeFromSuperview];
}

- (void)hiddenView
{
    // 【需求 8304】二维码页面移除的时候恢复之前的亮度
    if (self.isChangedBrightness) {
        [[UIScreen mainScreen] setBrightness:self.currentBrightness];
        self.isChangedBrightness = NO;
    }
    
    self.hidden = YES;
    [self removeFromSuperview];
}

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
    UIView* view = [super hitTest:point withEvent:event];
    CGPoint buttonPoint = [self.contentView convertPoint:point fromView:self];
    if (![self.contentView pointInside:buttonPoint withEvent:event]) {
        [self hiddenView];
    }
    return view;
}

- (void)createQRCodeWithString:(NSString *)string doorName:(NSString *)doorName needRefresh:(BOOL)needRefresh {
    self.refreshButton.hidden = YES;
    self.iconView.image = [SEGCreateQRCode createQRForString:string size:qrcodeImageWidth];
    
    if (string.length == 0 && needRefresh) {
        self.refreshButton.hidden = NO;
        self.iconView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"qrcode_fail")];
    } else {
        // 【需求8304】显示二维码时屏幕高亮
        // 保存当前的显示亮度
        self.currentBrightness = [UIScreen mainScreen].brightness;
        self.isChangedBrightness = YES;
        [[UIScreen mainScreen] setBrightness:1];
    }
    
    self.nameLabel.text = doorName;

    if ([SEGUtils isBlankString:doorName]) {
        self.nameLabel.hidden = YES;
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(636/2.0);
        }];
    }
}

- (void)refreshButtonTap:(UIButton *)sender {
    if (self.refreshBlock) {
        self.refreshBlock();
    }
}

@end
